$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/excel/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', width: 30, key: true},
            {label: '名称', name: 'name', width: 50},
            {label: '描述', name: 'remark', width: 200},
            {label: '地址', name: 'url', width: 200},
            {
                label: '上传', name: 'name', width: 100, formatter: function (value, options, row) {
                    return '<input type="button" onclick ="openForm(' + JSON.stringify(row).replace(/"/g, '&quot;') + ')" value="上传文件" />';
                }
            },
            {
                label: '模板', name: 'templateUrl', width: 80, formatter: function (value, options, row) {

                    return value == null ? "暂无模板" : '<a type="button" class="btn btn-success btn-sm" onclick ="downTemplate(' + value + ')" >模板下载</a>';
                }
            },

        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        multiboxonly: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });

    vm.initDept();
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showAdd: false,
        showList: true,
        title: null,
        q: {
            key: null
        },
        uploadBean: {
            id: null,
            deptIds: [],
            name: null,
            remark: null,
            url: null,

        },
        depts: []
    },
    methods: {
        query: function () {
            vm.reload();
        },
        reload: function () {
            vm.showAdd = false;
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }, add: function () {
            vm.showAdd = true;
            vm.showList = false;
            vm.uploadBean = {
                id: null,
                deptIds: [],
                name: null,
                remark: null,
                url: null,
            }
        }, initDept() {
            $.get(baseURL + "sys/dept/childList/3", r => {
                for (let i = 0; i < r.dept.length; i++) {
                    vm.$set(vm.depts, i, r.dept[i]);
                }
            })
        }, save: function () {
            var index;
            $.ajax({
                url: baseURL + "sys/excel/save",
                data: JSON.stringify(vm.uploadBean),
                type: "post",
                contentType: "application/json",
                beforeSend: function () {
                    index = layer.load(2, {
                        shade: [0.8, '#fff']
                    });
                },
                complete: function () {
                    layer.close(index);
                },
                success: function (r) {
                    layer.msg(r.msg)
                    vm.reload();
                }
            })
        }, update: function () {

            vm.showAdd = true;
            vm.showList = false;

            let rowid = getSelectedRow();
            if (rowid == null) {
                return;
            }
            let row = $("#jqGrid").jqGrid("getRowData", rowid);

            $.get(baseURL + "sys/excel/getById/" + row.id, r => {
                vm.uploadBean = r.uploadBean;
            });
        }, modify: function () {
            $.ajax({
                url: baseURL + "sys/excel/modify",
                data: JSON.stringify(vm.uploadBean),
                type: "post",
                contentType: "application/json",
                success: function (r) {
                    layer.msg(r.msg)
                    vm.reload();
                }
            })
        }, saveorupdate() {
            if (vm.uploadBean.id == null) {
                vm.save();
            } else {
                vm.modify();
            }


        }, deleteSelected() {

            let rowid = getSelectedRow();
            if (rowid == null) {
                return;
            }
            let row = $("#jqGrid").jqGrid("getRowData", rowid);


            $.get(baseURL + "sys/excel/deleteById/" + row.id, r => {
                layer.msg(r.msg)
                vm.reload();
            });
        }


    }
});

function openForm(row) {
    $("#file").val("");
    console.log(row)
    layer.open({
        type: 1,
        title: row.name,
        content: $("#upload"),
        area: ['500px', '200px'],
        btn: ['确定'],
        btn1: function (index) {
            uploadForm(row.url)
            layer.close(index);
        }
    })
}

function uploadForm(url) {
    let formData = new FormData($("#uploadForm")[0]);
    if ($("#tranfile").val() == "") {
        layer.msg("请选择文件");
        return;
    }
    formData.append("file", $("file"));
    let index;
    $.ajax({
        url: baseURL + url,
        type: 'POST',
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        beforeSend: function () {
            index = layer.load(2, {
                shade: [0.8, '#fff']
            });
        },
        complete: function () {
            layer.close(index);
            $("#file").val("");
        },
        success: function (r) {
            console.log(r)
            layer.msg(r.msg);
            $("#file").val("");
        },
        error: function (r) {
            alert(r.msg);
            $("#file").val("");
        }
    });
}

function downTemplate(value) {
    $.ajax({
        url: baseURL + "sys/excel/downTemplate",
        data: {"templateType": value},
        type: "post",
        dataType: "json",
        success: function (r) {
            if (r.code == 0) {
                window.location.href = r.url;
            } else {
                layer.msg(r.msg)
            }
        }
    })
}